package apps.reading;

import java.util.ArrayList;
import java.util.List;

import apps.reading.db.WebSourceBean;
import apps.reading.db.WebSourceService;
import core.tool.common.StringHelper;
import core.tool.common.SystemInfo;
import core.tool.scan.ScanService;

public class ParserAction {

	private List<WebSourceParser> webSourceParserList=new ArrayList<WebSourceParser>();
	
	WebSourceService webSourceService=(WebSourceService) SystemInfo.getBean("webSourceService");
	
	public void parser() throws Exception {
		ScanService scanService=new ScanService();
		scanService.scanPackage("apps.reading.spider", ReadingSpider.class);
		List<Object> annoList=scanService.annotationList;
		for(Object object:annoList) {
			WebSourceParser webSourceParser=(WebSourceParser) object;
			ReadingSpider readingSpider=(ReadingSpider) webSourceParser.getClass().getAnnotations()[0];
			List<WebSourceBean> list=webSourceParser.getWebSourceTitleList(readingSpider.url());
			for(WebSourceBean webSourceBean:list) {
				if(!webSourceService.existsTitle(webSourceBean.getTitle())) {
					WebSourceBean webSourceContent=webSourceParser.getContent(webSourceBean);
					webSourceContent.setId(StringHelper.getUUID());
					webSourceContent.setSourceType(readingSpider.sourceType());
					webSourceContent.setType(readingSpider.type());					
					webSourceService.save(webSourceContent);
				}					
			}
		}
	}
	
	
}
